<?php

namespace Common\Model;

/**
 * Description of RoomTypePriceModel
 * @author Paddy
 * @date 2016-11-25
 * @index roomtype_id(`roomtype_id`) roomtype_date(roomtype_id,price_date)
 */
class RoomTypePriceModel extends BaseModel {

    protected $trueTableName = 'tb_room_type_price';

    /**
     * 设置房型价格
     * @add Paddy 2016-11-25
     * @param integer $roomTypeId 房型ID
     * @param integer $date 时间戳
     * @param integer $price 价格
     * @return type
     */
    public function setPrice($roomTypeId, $date, $price) {
        $sql="INSERT INTO tb_room_type_price(`roomtype_id`,`price_date`,`price`) VALUES({$roomTypeId},{$date},{$price}) ON DUPLICATE KEY UPDATE `price`={$price}";
        $this->query($sql);
    }

    /**
     * 根据RoomTypeID获取价格设置列表
     * @add Paddy 2016-11-25
     * @param integer   $roomTypeId 房型ID
     * @param integer $startTime 结束时间戳
     * @param integer $endTime 结束时间戳
     * @return array               价格数组
     */
    public function getPriceList($roomTypeId, $startTime, $endTime) {
        $option = array();
        
        if (! is_array($roomTypeId)) {
            $roomTypeId = array( $roomTypeId );
        }
        $option['roomtype_id'] = array( 'in',  $roomTypeId );

        if (!is_null($startTime) && !is_null($endTime)) {
            $option['price_date'] = array(
                'between',
                array(
                    $startTime,
                    $endTime
                )
            );
        }
        
        $option['isdelete'] = NOT_DELETE;
        
        $data = $this->where($option)->field('roomtype_id,price_date,price')->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据RoomTypeID和当天日期或指定日期获取预定价格
     * @add wenhui 2016-12-21
     * @param integer   $roomTypeId 房型ID
     * @param integer $currentTime 当天日期
     * @param integer $startData 指定日期
     * @return array               价格数组
     */
    public function getBookPrice($roomTypeId, $startData = null) {
        $option = array();
        $currentTime = strtotime(date('Y-m-d'));
        if (! is_array($roomTypeId)) {
            $roomTypeId = array( $roomTypeId );
        }

        $option['price_date'] = $currentTime;

        if (!is_null($startData)) {
            $option['price_date'] = strtotime($startData);
        }

        $option['roomtype_id'] = array( 'in',  $roomTypeId );
        $option['isdelete'] = NOT_DELETE;

        $data = $this->where($option)->field('roomtype_id,price_date,price')->select();

        return empty($data) ? array() : $data;
    }
    /**
     * 房型预定价格
     * @modify zhangzhen 2017-3-3 创建
     * @param roomtype_id  房型id
     */
    public function getInfoByRoomType($roomtype_id,$priceDate) {
        $options = array(
            'roomtype_id' => $roomtype_id,
            'price_date' => $priceDate,
        );

        return $this->where($options)->order('id desc')->find();
    }
}

